﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class LayCleanMouth : HYExternalCommand
	{
		public override void FilterProcess()
		{
            base.FilterViewType.Remove((ViewType)2);
			base.FilterProcess();
		}

		public override Result HYExecute(ref string tranName)
		{
			tranName = "布置清扫口";
			FormCleanoutSetting formCleanoutSetting = new FormCleanoutSetting(base.Doc);
			YJKWindowHandle owner = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			string text = "";
			FamilySymbol familySymbol = YJKRevitTools.GetFamilySymbolByName("Accessory", "清扫口", base.Doc, ref text);
			string text2 = "";
			FamilySymbol familySymbolByName = YJKRevitTools.GetFamilySymbolByName("Fitting", "顺水三通", base.Doc, ref text2);
			bool isSSTee = false;
			if (familySymbolByName.Family.Name.Contains("顺水"))
			{
				isSSTee = true;
			}
			ISelectionFilter selectionFilter = new HoriPipeFilter();
			formCleanoutSetting.Show(owner);
			this.myForm = formCleanoutSetting;
			Result result;
			for (;;)
			{
				try
				{
					Reference reference = base.Sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "请选择一根横管");
					XYZ globalPoint = reference.GlobalPoint;
					Pipe pipe = base.Doc.GetElement(reference) as Pipe;
					YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
					double num = hypiping.GetDiameter();
					string b;
					if (UnitConvert.CovertFromAPI((Autodesk.Revit.DB.DisplayUnitType)2, num) < 74.0)
					{
						b = "DN50";
						num = UnitConvert.CovertToAPI(50.0, (Autodesk.Revit.DB.DisplayUnitType)2);
					}
					else if (UnitConvert.CovertFromAPI((Autodesk.Revit.DB.DisplayUnitType)2, num) < 99.0)
					{
						b = "DN75";
						num = UnitConvert.CovertToAPI(75.0, (Autodesk.Revit.DB.DisplayUnitType)2);
					}
					else if (UnitConvert.CovertFromAPI((Autodesk.Revit.DB.DisplayUnitType)2, num) < 149.0)
					{
						b = "DN100";
						num = UnitConvert.CovertToAPI(100.0, (Autodesk.Revit.DB.DisplayUnitType)2);
					}
					else
					{
						b = "DN150";
						num = UnitConvert.CovertToAPI(150.0, (Autodesk.Revit.DB.DisplayUnitType)2);
					}
					foreach (FamilySymbol familySymbol2 in familySymbol.Family.GetSymbols())
					{
						if (familySymbol2.Name == b)
						{
							familySymbol = familySymbol2;
							break;
						}
					}
					SubTransaction subTransaction = new SubTransaction(base.Doc);
					subTransaction.Start();
					XYZ project = hypiping.GetProject(globalPoint, false);
					XYZ xyz;
					if (formCleanoutSetting.setReference == 0)
					{
						xyz = new XYZ(project.X, project.Y, formCleanoutSetting.heigh);
					}
					else
					{
						xyz = new XYZ(project.X, project.Y, formCleanoutSetting.heigh + project.Z - base.Doc.ActiveView.GenLevel.ProjectElevation);
					}
					UnifiedModified.ActivateFamilySymbol(familySymbol);
					FamilyInstance familyInstance = base.Doc.Create.NewFamilyInstance(xyz, familySymbol, base.Doc.ActiveView.GenLevel, (Autodesk.Revit.DB.Structure.StructuralType)0);
					base.Doc.Regenerate();
					if (familyInstance == null)
					{
						formCleanoutSetting.Close();
						result = Autodesk.Revit.UI.Result.Failed;
					}
					else
					{
						if (project.Z > xyz.Z + base.Doc.ActiveView.GenLevel.ProjectElevation - pipe.Diameter * 2.5)
						{
							YJKMessageBox.Warning("清扫口的位置不合适！");
							subTransaction.RollBack();
							continue;
						}
						Connector elementConnector = YJKRevitTools.GetElementConnector(familyInstance);
						new HYCreatePipeHelper();
						Pipe pipe2 = YJKRevitTools.NewPipe(pipe, project, elementConnector.Origin);
						pipe2.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set(num);
						YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
						string a = "";
						hypipeConnectorHelper.TPipeConnectRPipe(pipe2, pipe, base.Doc, familySymbolByName, isSSTee, 2, ref a, false);
						if (a != "")
						{
							YJKMessageBox.Show("该位置不能布置清扫口");
							subTransaction.RollBack();
							continue;
						}
						YJKRevitTools.ConnectTwoElement(familyInstance, pipe2);
						subTransaction.Commit();
						continue;
					}
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException)
				{
					formCleanoutSetting.Close();
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
				catch (Exception ex)
				{
					formCleanoutSetting.Close();
					YJKMessageBox.Show(ex.Message);
					result = Autodesk.Revit.UI.Result.Failed;
				}
				break;
			}
			return result;
		}

		public override void TailProcess()
		{
			if (this.myForm != null && !this.myForm.IsDisposed)
			{
				this.myForm.Close();
			}
			base.TailProcess();
		}

		public PipingSystemType Get_PipingSystemType(Autodesk.Revit.DB.Document document, string pipingSystemTypeName)
		{
			ICollection<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(PipingSystemType), document);
			return this.GetElementSameName(elementByClass, pipingSystemTypeName) as PipingSystemType;
		}

		private Element GetElementSameName(ICollection<Element> collection, string elemName)
		{
			Element result = null;
			foreach (Element element in collection)
			{
				if (element != null && element.Name == elemName)
				{
					result = element;
				}
			}
			return result;
		}

		private System.Windows.Forms.Form myForm;
	}
}
